package com.yy.tech.data.coder.decode;

import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.StdConverter;
import com.yy.tech.data.coder.secret.std.Aes256Secret;
import com.yy.tech.data.coder.secret.std.Base64Secret;
import com.yy.tech.data.context.SecretContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yy
 * @version 1.0.0
 * @date 2022/4/4
 * @description: TODO
 */
public interface Decoder<IN, OUT> extends Converter<IN, OUT> {

    default OUT decode(IN in){
        return convert(in);
    }

    @Component(SecretContent.DECODER_BASE64)
    class Base64Decoder extends StdConverter<String,String> implements Decoder<String,String> {

        @Autowired
        private Base64Secret secret;
        @Override
        public String convert(final String str) {
            return secret.decrypt(str);
        }
    }

    @Component(SecretContent.DECODER_AES256)
    class Aes256Decoder extends StdConverter<String,String> implements Decoder<String,String> {

        @Autowired
        private Aes256Secret secret;
        @Override
        public String convert(final String str) {
            return secret.decrypt(str);
        }
    }

}
